﻿using Synthesize.BaseSet.Domain.Aggregation.Dtos.Authority.Role;
using Synthesize.BaseSet.Domain.Repository.Authority;
using Synthesize.BaseSet.ServiceInterface.Dtos.Authority.Role;
using Synthesize.BaseSet.ServiceInterface.Query.Authority;
using Library.Common.Model;
using Library.Core.Application.BaseAction;
using Library.Core.Application.IInfrastructure;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using Synthesize.BaseSet.Domain.Specifications.Authority.Role;
using Library.Core.Domain.Repository;
using System.Linq;
using Library.Common.Serialize;
using Synthesize.BaseSet.Domain.Aggregation.Entity.Authority;

namespace Synthesize.BaseSet.Application.Query.Authority
{
    /// <summary>
    /// 角色查询逻辑
    /// </summary>
    public class RoleQueryService : BaseService, IRoleQueryService
    {
        private readonly IRoleRepository _roleRepository;

        public RoleQueryService(IIocContainer container, IRoleRepository roleRepository) : base(container)
        {
            _roleRepository = roleRepository;
        }

        /// <summary>
        /// 获取角色分页列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<BaseApiResult<PageData<GetRoleListOutput>>> GetRolePageList(GetRoleListInput input)
        {
            return await HandleAsync(input, async () =>
            {
                var result = new PageData<GetRoleListOutput>();

                var orderExp = input.OrderParms.ConvertToOrderExpression<GetRoleListOutDto>();
                var spceifications = new GetRoleListSpceification(input.Keywords).SatisfiedBy();
                var list = await _roleRepository.GetRolePageList(input.Page, input.PageSize,spceifications, orderExp,input.ReadOnly);
                if (list.Rows.Any())
                {
                    result.Rows = MapperHelper<GetRoleListOutDto, GetRoleListOutput>.MapList(list.Rows);
                    result.Page = input.Page;
                    result.PageSize = input.PageSize;
                    result.Total= list.Total;
                }

                return result;
            });
        }

        /// <summary>
        /// 获取所有角色列表信息
        /// </summary>
        /// <returns></returns>
        public async Task<BaseApiResult<List<GetRoleListOutput>>> GetAllList() {

            return await HandleAsync(async () =>
            {
                var result = new List<GetRoleListOutput>();
                var list = await _roleRepository.GetManyAsync(x=>true);
                if (list.Any())
                {
                    result = MapperHelper<RoleInfoEntity, GetRoleListOutput>.MapList(list);
                }
                return result;
            });
        }

        /// <summary>
        /// 根据用户Id获取所属角色
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="readOnly"></param>
        /// <returns></returns>
        public async Task<List<GetRoleListOutput>> GetRoleByUserId(Guid userId, bool readOnly = true)
        {
            var result = new List<GetRoleListOutput>();

            var roleInfos = await _roleRepository.GetRoleByUserId(userId, readOnly);
            if (roleInfos.Any())
            {
                result = MapperHelper<GetRoleListOutDto, GetRoleListOutput>.MapList(roleInfos);
            }
            return result;
        }
    }
}
